package com.tuanzi.loan.web.bean.wallet;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.wallet.LoanUserWallet;
import com.tuanzi.loan.business.entity.wallet.LoanUserWalletFlow;
import com.tuanzi.loan.business.entity.wallet.LoanUserWalletWithdrawRecord;
import com.tuanzi.loan.business.service.wallet.LoanUserWalletFlowService;
import com.tuanzi.loan.business.service.wallet.LoanUserWalletService;
import com.tuanzi.loan.business.service.wallet.LoanUserWalletWithdrawRecordService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.wallet.LoanUserWalletWithdrawRecordRequest;
import com.tuanzi.loan.vo.response.wallet.LoanUserWalletWithdrawRecordResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.wallet.LoanUserWalletWithdrawRecordLazyDataModel;
import com.tuanzi.server.loan_base.constant.wallet.LoanUserWalletWithdrawRecordStatus;

/**
 * 用户钱包提现明细管理后台Bean
 * @author chenweijun 2018/02/21
 *
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanUserWalletWithdrawRecordBean extends JSFListingBean<LoanUserWalletWithdrawRecordRequest, LoanUserWalletWithdrawRecordResponse> {
	 
	private static final long serialVersionUID = 4135628593105713356L;
	
	@Autowired
	private LoanUserWalletWithdrawRecordService loanUserWalletWithdrawRecordService;
	
	@Autowired
	private LoanUserWalletService loanUserWalletService;
	
	@Autowired
	private LoanUserWalletFlowService loanUserWalletFlowService;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}
	
	private void initLazyDataModel() {
		$ = new LoanUserWalletWithdrawRecordRequest();
		lazyDataModel = new LoanUserWalletWithdrawRecordLazyDataModel($);
	}
	
	/**
	 * 回退提现余额到钱包
	 * @param id
	 */
	public void returnBalance(Long id){
		
		LoanUserWalletWithdrawRecord record = loanUserWalletWithdrawRecordService.findOne(id);
		Float amount = record.getAmount();
		Long userId = record.getUserId();
		Integer withdrawType = record.getWithdrawType();
		
		//1.修改提现订单为已退款状态
		record.setStatus(LoanUserWalletWithdrawRecordStatus.REFUND);
		record.setUpdator(CurrentUser.get().getName());
		record.setUpdateTime(new Date());
		loanUserWalletWithdrawRecordService.saveOrUpdate(record);
		
		//2.修改用户钱包余额
		LoanUserWallet wallet = loanUserWalletService.findByUserId(userId);
		Float balance = wallet.getBalance() + Math.abs(amount);
		wallet.setBalance(balance);
		wallet.setUpdator(CurrentUser.get().getName());
		wallet.setUpdateTime(new Date());
		loanUserWalletService.saveOrUpdate(wallet);
		
		//3.增加钱包操作流水
		LoanUserWalletFlow flow = new LoanUserWalletFlow();
		flow.setUserId(userId);
		flow.setAmount(amount);
		flow.setBalance(balance);
		String remark = translateWithdrawType(withdrawType) + "回退";
		flow.setRemark(remark);
		flow.setCreateTime(new Date());
		flow.setCreator(CurrentUser.get().getName());
		
		loanUserWalletFlowService.saveOrUpdate(flow);

	}
	
	
	public String translateWithdrawType(Integer withdrawType){
		switch (withdrawType) {
			case 1:
				return "支付宝转账";
	
			case 2:
				return "微信红包";
				
			case 3:
				return "话费充值";
				
			default:
				return "未定义";
		}
	}
	
	
	public String translateStatus(Integer status){
		
		switch (status) {
			case 0:
				return "待发放";
				
			case 1:
				return "已发放";
				
			case -1:
				return "发放失败";
				
			case 2:
				return "已领取";
				
			case -2:
				return "已失效";
			
			case -3:
				return "已退款";
				
			default:
				return "未定义";
		}
		
	}
	
	
}
